package com.zhangsun.system.controller;


import com.zhangsun.base.bean.ResponseData;
import com.zhangsun.common.utils.ResponseUtil;
import com.zhangsun.system.entity.DictEntity;
import com.zhangsun.system.service.DictService;
import com.zhangsun.system.vo.dict.DictEditVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 数据字典 前端控制器
 * </p>
 *
 * @author zhangsun
 * @since 2019-08-13
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    /**
     * 获取数据字典
     *
     * @param code 分类
     * @return 字典
     */
    @GetMapping("/findAll/{code}")
    public ResponseData findAllByCode(@NotBlank @PathVariable String code) {
        List<DictEntity> dictList = dictService.findAllByCode(code);
        return ResponseUtil.success(dictList);
    }

    /**
     * 添加字典
     *
     * @param vo 字典
     * @return 结果
     */
    @PostMapping("/create")
    @RequiresPermissions("system:dict:edit")
    public ResponseData create(@Validated DictEditVo vo) {
        dictService.create(vo);
        return ResponseUtil.success();
    }

    /**
     * 获取详情
     *
     * @param id 字典id
     * @return 详情
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:dict:view")
    public ResponseData findById(@NotBlank @PathVariable String id) {
        DictEntity dictEntity = dictService.findById(id);
        return ResponseUtil.success(dictEntity);
    }

    /**
     * 更新字典
     *
     * @param id 字典id
     * @param vo 字典
     * @return 结果
     */
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:dict:edit")
    public ResponseData update(@NotBlank @PathVariable String id, @Validated DictEditVo vo) {
        dictService.update(id, vo);
        return ResponseUtil.success();
    }

    /**
     * 删除字典
     *
     * @param id 字典id
     * @return 结果
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:dict:delete")
    public ResponseData delete(@NotBlank @PathVariable String id) {
        dictService.delete(id);
        return ResponseUtil.success();
    }

}
